package com.nctu.dm.fpgrowth;

import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class TrajectoryDataset {
	
	private TransactionTable ConvertedMovieLensDataset = new TransactionTable("root",-1);
	public TrajectoryDataset(String filepath){
	    // Here BufferedInputStream is added for fast reading.
	   
	    	
	    	JSONObject obj;
	    	try {
				obj = new JSONObject(new JSONTokener(new FileReader(new File(filepath))));
				
				 if (obj != null)   {
			            // Print the content on the console	        	
			            __ConfigCMLD(obj);
			        };
			      //  br.close();
			        
			        __CreateCLMD();
				
				
			} catch (JSONException | FileNotFoundException e) {
				e.printStackTrace();
			}

	       
	        
	  
	    
	}
	private void __ConfigCMLD(JSONObject obj){
	
        JSONArray userArray;
        JSONArray trajArray;
        JSONArray locaArray;
     

        
		userArray = obj.getJSONArray("users");
		System.out.println(userArray+"\n");
		 
		 
		 for (int i = 0; i < userArray.length(); i++) {
			 
			 trajArray = obj.getJSONArray("users").getJSONObject(i).getJSONArray("trajectories");
		
			 
		
			 for (int j = 0; j < trajArray.length(); j++) {
				 
				 locaArray = obj.getJSONArray("users").getJSONObject(i).getJSONArray("trajectories").getJSONArray(j);
				 int item=0;
				
				 Collection<String> cs = new ArrayList<String>();
				 cs.add("trajectory"+ String.valueOf(i)+ String.valueOf(j));
				 Iterator<String> iter = cs.iterator();
				 while(iter.hasNext()){
					 String test=iter.next();
				 System.out.println(test+"\n"); 
				
			 }
				
				 
				 
				 for (int k = 0; k < locaArray.length(); k++) {
					 
					 
					 String[] temp =  new String[locaArray.length()];
					 temp[k] = locaArray.getJSONArray(k).getString(0);
					  
					 if(!cs.contains(temp[k])){
						 System.out.println(cs.contains(temp[k])+"\n");
						 item++;
						 cs.add(temp[k]);
						  
						 System.out.println(k+"th item:"+locaArray.getJSONArray(k).getString(0)+"\n in SA's"+item+"\n");
						 Iterator<String> itera = cs.iterator();

					
					 }
					  
					 
				 }
				 
				 String[] sa = (String[]) cs.toArray(new String[0]);
				
				 
				 for(int m=1;m<sa.length;m++){
					
					 if(Common.Debug) System.out.println(sa[0]+','+sa[m]+"\n"); 
					 if(ConvertedMovieLensDataset.Table.containsKey(sa[0]))
							ConvertedMovieLensDataset.addTransaction(sa[0], sa[m]);
						else{
							Vector<String> v = new Vector<String>();
							v.add(sa[m]);
							ConvertedMovieLensDataset.addTransaction(sa[0], v, 1);
						}
					 
				 }
				 
			 
		 }
		
		
		
		 }  
        


	}
	
	
	private void __CreateCLMD()
	{
        //Make the CMLD file
		try{
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new DataOutputStream(new FileOutputStream(Common.CMLD_NAME))));
			for( Map.Entry <String,TransactionValuePair> i:ConvertedMovieLensDataset.Table.entrySet()){
				bw.write(i.getKey()+'\t');
			
				
				//bw.flush();
				for(int j = 0;j<i.getValue().trans.size();j++){
				
					if(j== i.getValue().trans.size()-1 ) bw.write(i.getValue().trans.get(j)); 
					else bw.write(i.getValue().trans.get(j)+',');	
					
					//bw.flush();
				}
				bw.write('\n');
				//bw.flush();
			}
			bw.close();
		}catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	    	e.printStackTrace();
	    }	 
        
	}
	
	public void PrintConvertedMovieLensDataset(){
		for( Map.Entry <String, TransactionValuePair> i:ConvertedMovieLensDataset.Table.entrySet()){
			System.out.printf("%s\t",i.getKey());
			System.out.flush();
			for(int j = 0;j<i.getValue().trans.size();j++){									
				if(j== i.getValue().trans.size()-1 ){ 
					System.out.printf("%s",i.getValue().trans.get(j));
					System.out.flush();
				}
				else{
					System.out.printf(i.getValue().trans.get(j));
					System.out.printf(",");
					System.out.flush();
				}
				System.out.flush();
			}
			System.out.println();
			System.out.flush();
			
		}
	}
	
	public TransactionTable GetCMLD(){
		return ConvertedMovieLensDataset;
	}
}


